<?php
namespace app\common\command;

use think\Db;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
use app\common\model\RentDetails;

class Rent extends Command{
    
    protected function configure()
    {
        $this->setName('rent')->setDescription('rent');
    }
    
    protected function execute(Input $input, Output $output)
    {
        $sTime = microtime(true);
        self::account();
        $eTime = microtime(true);
        $time = $eTime - $sTime;
        $output->writeln('消耗时间'.$time.'秒');
    }
    
    protected static function account(){
       Db::name('rent_details')->where(['delete_time'=>null,'status'=>1])->chunk(100, function($data) {
           foreach ($data as $value) {
              $month = strtotime(date('Y-m',time()));
              //$month = strtotime('2019-05');
              $expireTime = strtotime(date('Y-m',$value['expire_time']));
              if($expireTime >= $month){
                  if($month > $value['belongs_month']){
                      $startTime = mktime(0, 0, 0, date("m",$value['end_time']), date("d",$value['end_time'])+1, date("Y",$value['end_time']));
                      $endTime =mktime(0, 0, 0, date("m",$startTime), date("d",$startTime)+30, date("Y",$startTime));
                      
                      $accountData = [
                          'user_id'=>$value['user_id'],
                          'agency_id'=>$value['agency_id'],
                          'brand_id'=>$value['brand_id'],
                          'contract_number'=>$value['contract_number'],
                          'type'=>$value['type'],
                          'rent_money'=>$value['rent_money'],
                          'service_money'=>$value['service_money'],
                          'rent_price'=>$value['rent_money'] + $value['service_money'],
                          'number'=>$value['number'],
                          'contract_money'=>$value['contract_money'],
                          'rent_man'=>$value['rent_man'],
                          'contract_time'=>$value['contract_time'],
                          'expire_time'=>$value['expire_time'],
                          'rent_number'=>$value['rent_number'],
                          'ought_time'=>$startTime,
                          'belongs_month'=>date("Y-m",mktime(0, 0, 0, date("m",$value['belongs_month'])+1, 1, date("Y",$value['belongs_month']))),
                          'start_time'=>$startTime,
                          'end_time'=>$endTime,
                          'status'=>1
                      ]; 
    
                    try {
                         RentDetails::create($accountData);
                         RentDetails::update(['status'=>0],['id'=>$value['id']]);
                     } catch (\Exception $e) {
                         Log::record('[ 插入数据 ]:'.$e->getMessage(),'error');
                     }  
                      
                  }
              }
            }  
            
        });
    }
}